package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.model.mapper.SuprAuthMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.dto.KghImg;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.KAKAAPI;
import com.ruoyi.system.outto.game.WUYOUAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class KAKAAPIImpl implements KAKAAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Resource
    private SuprAuthMapper suprAuthMapper;
    @Resource
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;

    @Override
    public AjaxResult addGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(accountInfo, "AddGame",salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm(salePartner.getOther(), params,null);
        if(salePartner.getName().contains("卡卡")){
            log.info("卡卡添加接口请求参数={}", JSONObject.toJSONString(params));
            log.info("卡卡添加接口返回参数={}", response);
        }else {
            log.info("多多添加接口请求参数={}", JSONObject.toJSONString(params));
            log.info("多多添加接口返回参数={}", response);
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"200".equals(resultJson.getString("status"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", MapUntils.unicodeToString(resultJson.getString("msg")));
        System.out.println("返回信息:"+MapUntils.unicodeToString(resultJson.getString("msg")));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(accountInfo, "EditGame",salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm(salePartner.getOther(), params,null);
        if(salePartner.getName().contains("卡卡")){
            log.info("卡卡编辑接口请求参数={}", JSONObject.toJSONString(params));
            log.info("卡卡编辑接口返回参数={}", response);
        }else {
            log.info("多多编辑接口请求参数={}", JSONObject.toJSONString(params));
            log.info("多多编辑接口返回参数={}", response);
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"200".equals(resultJson.getString("status"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", MapUntils.unicodeToString(resultJson.getString("msg")));
        System.out.println("返回信息:"+MapUntils.unicodeToString(resultJson.getString("msg")));
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(AccountInfo accountInfo, String type,SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("type",type);
        params.put("catid", "8342BB6C12B595E0");
        //游戏大区
        if(accountInfo.getRegional().contains("苹果Q")){
            params.put("catid", 6);
            params.put("daqu","QQ苹果全区");
        }
        if(accountInfo.getRegional().contains("安卓Q")){
            if(salePartner.getName().contains("卡卡")) {
                params.put("catid", 4);
            }else{
                params.put("catid", 7);
            }
            params.put("daqu","QQ安卓全区");
        }
        if(accountInfo.getRegional().contains("安卓WX")){
            if(salePartner.getName().contains("卡卡")) {
                params.put("catid", 5);
            }else{
                params.put("catid", 9);
            }
            params.put("daqu","微信安卓全区");
        }
        if(accountInfo.getRegional().contains("苹果WX")){
            if(salePartner.getName().contains("卡卡")) {
                params.put("catid", 7);
            }else{
                params.put("catid", 8);
            }
            params.put("daqu","微信苹果全区");
        }
        params.put("title", descTitle+accountInfo.getId()+accountInfo.getDes().substring(0,50));
        params.put("thumb",accountInfo.getTitleImg());
        params.put("username","wutong");
        params.put("ztdw","青铜");
        params.put("ztpf",accountInfo.getSkins());
        params.put("ztmw",300);
        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(), salePartner.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(),salePartner);
        params.put("ztprice",bigDecimal);
        params.put("dengji","30");
        //皮肤
        String pifu = "";
        String response1 = neice(salePartner.getTwoOther());
        JSONArray neice = JSONArray.parseArray(response1);
        for(Object o : neice){
            if(StringUtils.isNotEmpty(JSONObject.parseObject(o.toString()).getString("name"))) {
                if (accountInfo.getDes().contains(JSONObject.parseObject(o.toString()).getString("name"))) {
                    pifu += "," + JSONObject.parseObject(o.toString()).getString("name");
                }
            }
        }
        String response2 = rongyaoshuijing(salePartner.getTwoOther());
        JSONArray rongyaoshuijing = JSONArray.parseArray(response2);
        for(Object o : rongyaoshuijing){
            if(accountInfo.getDes().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                pifu += "," + JSONObject.parseObject(o.toString()).getString("name");
            }
        }
        String response3 = xingyuanpifu(salePartner.getTwoOther());
        JSONArray xingyuanpifu = JSONArray.parseArray(response3);
        for(Object o : xingyuanpifu){
            if(accountInfo.getDes().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                pifu += "," + JSONObject.parseObject(o.toString()).getString("name");
            }
        }
        String response4 = xiyoupifu(salePartner.getTwoOther());
        JSONArray xiyoupifu = JSONArray.parseArray(response4);
        for(Object o : xiyoupifu){
            if(accountInfo.getDes().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                pifu += "," + JSONObject.parseObject(o.toString()).getString("name");
            }
        }
        String response5 = shishichuanshuo(salePartner.getTwoOther());
        JSONArray shishichuanshuo = JSONArray.parseArray(response5);
        for(Object o : shishichuanshuo){
            if(accountInfo.getDes().contains(JSONObject.parseObject(o.toString()).getString("name"))){
                pifu += "," + JSONObject.parseObject(o.toString()).getString("name");
            }
        }
        if(StringUtils.isNotEmpty(pifu)) {
            params.put("pifu", pifu.substring(1));
        }
        if(salePartner.getName().contains("卡卡")) {
            params.put("product_id", accountInfo.getId());
        }else{
            params.put("tianxai_id", accountInfo.getId());
        }
        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);
        String images = "";
        for(int i = 0;i<infoImgs.size();i++){
            images+=","+infoImgs.get(i).getImgUrl();
        }
        if(StringUtils.isNotEmpty(images)) {
            params.put("images", images.substring(1));
        }else{
            params.put("images",accountInfo.getTitleImg());
        }
        //sign
        String cover = MapUntils.getCover(params);
        cover = cover + "key="+salePartner.getSaleKey();
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
        return params;
    }

    @Override
    public AjaxResult delGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = new HashMap<>();
        params.put("type","DelGame");
        params.put("username","wutong");
        if(salePartner.getName().contains("卡卡")) {
            params.put("product_id", accountInfo.getId());
        }else{
            params.put("tianxai_id", accountInfo.getId());
        }
        //sign
        String cover = MapUntils.getCover(params);
        cover = cover + "key="+salePartner.getSaleKey();
        System.out.println(cover);
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign",sign);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm(salePartner.getOther(), params,null);
        if(salePartner.getName().contains("卡卡")){
            log.info("卡卡删除接口请求参数={}", JSONObject.toJSONString(params));
            log.info("卡卡删除接口返回参数={}", response);
        }else {
            log.info("多多删除接口请求参数={}", JSONObject.toJSONString(params));
            log.info("多多删除接口返回参数={}", response);
        }
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"200".equals(resultJson.getString("status"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", MapUntils.unicodeToString(resultJson.getString("msg")));
        System.out.println("返回信息:"+MapUntils.unicodeToString(resultJson.getString("msg")));
        return AjaxResult.success(result);
    }

    public static void main(String[] args) {
        String neice = neice("http://kaka.syzhmm.com/ajax.php");
        System.out.println(neice);
        /*String cover = "{\"supplier\":\"糖豆\",\"DataJson\":\"{\\\"createtime\\\":1670330973789,\\\"creator\\\":\\\"糖豆\\\",\\\"areaname\\\":\\\"官服\\\",\\\"gamename\\\":\\\"原神\\\",\\\"introduce\\\":\\\"不送邮箱,TDYY19 【冒险等级53】【14黄】神里绫人.荒泷一斗.可莉.心海.一命莫娜.一命琴.薙草之稻光.无工之剑.四风原典.天空之翼.阿莫斯之弓.天空之脊.\\\",\\\"bk3\\\":\\\"3\\\",\\\"title\\\":\\\"不送邮箱,TDYY19 【冒险等级53】【14黄】神里绫人.荒泷一斗.可莉.心海.一命莫娜.一命琴.薙草之稻光.无工之剑.四风原典.天空之翼.阿莫斯之弓.天空之脊.\\\",\\\"content\\\":\\\" <img src=\\\\\\\"http://img.taoqiyou.com/common/3533b7c1-94cc-47b0-8d3a-a7cf747ca108.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/1c89e071-87d2-4604-afc0-f5e706f06a33.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/683979d8-f9f0-4e68-9437-844e1610f703.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/33d9c616-9fd2-4073-88c1-cefbb1158357.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/e835827b-e51e-4e44-8efd-7e5d081b0220.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/3c145fc5-385c-4e88-9143-ecf84e56ed46.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/120c8f84-9fcd-4e80-899c-001ec5068e03.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/aff168c6-f8f6-42d8-8204-9e33def8941a.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/0cabb855-164e-4fae-877d-9476ee49b9e2.png\\\\\\\" /><img src=\\\\\\\"http://img.taoqiyou.com/common/6a4074ef-99e4-4b07-b1cf-dab56210dd36.png\\\\\\\" />\\\",\\\"pic1\\\":\\\"http://img.taoqiyou.com/common/3533b7c1-94cc-47b0-8d3a-a7cf747ca108.png\\\",\\\"price\\\":500.00,\\\"state\\\":\\\"1\\\",\\\"updatetime\\\":1670330973789,\\\"pic2\\\":\\\"http://img.taoqiyou.com/common/3533b7c1-94cc-47b0-8d3a-a7cf747ca108.png\\\",\\\"bprice\\\":500.00,\\\"id\\\":131153}\",\"Method\":\"Gamereceive\"}";

        System.out.println(MapUntils.unicodeToString(JSONObject.parseObject(cover));*/
    }

    public static String neice(String https){
        Map<String, Object> params1 = new TreeMap<>();
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(https+"?action=fiedles&type=neice", params1,null);
        return response;
    }
    public static String rongyaoshuijing(String https){
        Map<String, Object> params1 = new TreeMap<>();
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(https+"?action=fiedles&type=rongyaoshuijing", params1,null);
        return response;
    }
    public static String xingyuanpifu(String https){
        Map<String, Object> params1 = new TreeMap<>();
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(https+"?action=fiedles&type=xingyuanpifu", params1,null);
        return response;
    }
    public static String xiyoupifu(String https){
        Map<String, Object> params1 = new TreeMap<>();
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(https+"?action=fiedles&type=xiyoupifu", params1,null);
        return response;
    }
    public static String shishichuanshuo(String https){
        Map<String, Object> params1 = new TreeMap<>();
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm(https+"?action=fiedles&type=shishichuanshuo", params1,null);
        return response;
    }

}
